<?php

class IndexController extends Zend_Controller_Action {

    protected $_netwerk;

    public function init() {
        if(!$this->getRequest()->isXmlHttpRequest()){
            $this->_netwerk = new Default_Model_DbTable_Netwerkstatus();
            $where = $this->_netwerk->select()->where('startDate < ?', new Zend_Db_Expr("now()"))->where('endDate > ?', new Zend_Db_Expr("now()"));
            $this->view->netwerk = $this->_netwerk->fetchAll($where)->toArray();

            $response = $this->getResponse();
            $response->insert('sidebar', $this->view->render('sidebar.phtml'));
        }
        
    }

    protected function indexAction() {

    }
    protected function aboutAction() {

    }
    protected function portfolioAction() {
        
        
    }
    protected function klantenAction() {
        $klanten = new Default_Model_DbTable_Klanten();

        $this->view->klanten = $klanten->fetchAll($klanten->select()->where('datum < now()')->order('datum DESC'));
    }
    protected function contactAction() {
        $form = $this->_helper->form('contact');

        if($this->getRequest()->isPost() && !$this->getRequest()->isXmlHttpRequest()) {

            if($form->isValid($this->getRequest()->getPost())) {
                
                Tc_ContactMailer::send($form->getValues());
                $this->view->response = "Bedankt voor uw bericht! wij nemen contact met u op.";

            }else {
                $this->view->form = $form;
            }
        }elseif($this->getRequest()->isPost() && $this->getRequest()->isXmlHttpRequest()){
            $this->getHelper('viewRenderer')->setNoRender();
            $this->getHelper('layout')->disableLayout();

            if($form->isValid($this->getRequest()->getPost())) {
                Tc_ContactMailer::send($form->getValues());
                echo Zend_Json_Encoder::encode('prima');
            }else{

                $errors = array();
                foreach($form->getErrors() as $name=>$error){
                    if(count($error)){
                        $errors[] = $name;
                    }
                }
                echo Zend_Json_Encoder::encode($errors);
            }
        }else {
            $this->view->form = $form;
        }

    }

    protected function supportAction() {

    }
}




